﻿using System;
using System.Linq;

public partial class administrator_add_news : System.Web.UI.Page
{
    dulieuDataContext db=new dulieuDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FCK.Language = "vi";
            var fileBrowser = new CKFinder.FileBrowser();
            fileBrowser.BasePath = "../ckfinder/";
            fileBrowser.SetupCKEditor(FCK);
            LoadNhomTin();
        }
    }
    protected void LoadNhomTin()
    {
        drdlnhomtin.DataSource = from o in db.Cates where o.TypeCateID == 1 && o.Parent == 0 select o;
        drdlnhomtin.DataTextField = "Name";
        drdlnhomtin.DataValueField = "Cate_ID";
        drdlnhomtin.DataBind();
    }
    protected void imbtthem_Click(object sender, System.Web.UI.ImageClickEventArgs e)
    {
        var news = new New()
                  {
                      Cate_ID = Convert.ToInt32(drdlnhomtin.SelectedValue),
                      Title = txttieude.Text,
                      Quote = txttomtat.Text,
                      Contents = FCK.Text,
                      Image = GetFilePic(),
                      CreateTime = DateTime.Now,
                      CreateBy = "admin"
                  };
        db.News.InsertOnSubmit(news);
        db.SubmitChanges();
        lblthongbao.Text = "Đã đăng tin thành công";
    }
    public string GetFilePic()
    {
        var sfile = "";
        if (FileUpload1.HasFile)
        {
            sfile = Guid.NewGuid().ToString().Substring(0, 10) + FileUpload1.PostedFile.FileName.Remove(0, FileUpload1.PostedFile.FileName.LastIndexOf("."));
            FileUpload1.PostedFile.SaveAs(Server.MapPath("~/images/") + sfile);
        }
        return sfile;
    }
}